<?php

namespace App\Repositories\Address;

//use App\Address\Validators\UserAddressValidator;
use App\Criteria\OrderInfo\GetByUserIdCriteria;
use Illuminate\Support\Facades\DB;
use Prettus\Repository\Eloquent\BaseRepository;
use App\Models\Address\UserAddress;

/**
 * Class UserAddressRepositoryEloquent
 * @package namespace App\Repositories\Address;
 */
class UserAddressRepositoryEloquent extends BaseRepository implements UserAddressRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return UserAddress::class;
    }

    /**
     * @return null|string
     */
    public function validator()
    {

        //return UserAddressValidator::class;
    }

    /**
     * 获取用户默认地址
     * @param int $userId
     * @return mixed
     */
    public function getDefaultAddress(int $userId){
        return $this->findWhere(['user_id'=>$userId,'default'=>1],['address_id','address_name','user_id','consignee','province','city',
                'district','address','mobile','sign_building','longitude','latitude',
                'sex','address_tag','is_ok']
        );
    }

    /**
     * 获取单个地址详情
     * @param int $id
     * @return mixed
     */
    public function getAddressById(int $id){
        return $this->find($id,
            ['address_id','address_name','user_id','consignee','province','city',
                'district','address','mobile','sign_building','longitude','latitude',
                'sex','address_tag','is_ok']
        );
    }

    /**
     * 获取地址列表
     * @param int $userId
     * @param int $page
     * @return mixed
     * @throws \Prettus\Repository\Exceptions\RepositoryException
     */
    public function getAddressList(int $userId, int $page = 1){
        $this->pushCriteria(new GetByUserIdCriteria($userId));
        $_GET['page'] = $page;  //赋值当前页数
        return $this->simplePaginate();
    }

    /**
     * 新加地址
     * @param array $addressInfo
     * @return mixed
     * @throws \Prettus\Validator\Exceptions\ValidatorException
     */
    public function addAddress(array  $addressInfo){
        return $this->create($addressInfo);
    }

    /**
     * 修改地址
     * @param array $addressInfo
     * @param int $addressId
     * @return mixed
     * @throws \Prettus\Validator\Exceptions\ValidatorException
     */
    public function updateAddress(array $addressInfo, int $addressId){
        return $this->update($addressInfo,$addressId);
    }

    /**
     * 删除地址
     * @param int $addressId
     * @return int
     */
    public function deleteAddress(int $addressId){
        return $this->delete($addressId);
    }

    public function setDefaultAddress(int $addressId){
//        $this->findWhere([''], $columns = ['*'])
        DB::table('user_address')->where(['user'=>1])->update(['default'=>0]);

        return $this->update(['default'=>1],$addressId);
    }
    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        //$this->pushCriteria(app(RequestCriteria::class));
    }
}
